package com.scale.scales;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scale.scales.adapter.FoodAttriAdapter;
import com.scale.scales.adapter.IndexAdapter;
import com.scale.scales.bean.FoodAttributesBean;
import com.scale.scales.bean.FoodBean;
import com.scale.scales.bean.FoodInItBean;
import com.scale.scales.manager.DBManager;
import com.scale.scales.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ScalesActivity extends BaseActivity {

    @BindView(R.id.show_analysis)
    public RecyclerView show_analysis;
    List<FoodAttributesBean> foodAttributesBeanList =new ArrayList<>();
    private FoodAttriAdapter foodAttriAdapter;
    private DBManager dbManager;
    Gson gson =new Gson();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    @Override
    protected int intiLayout() {
        return R.layout.activity_scales;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbManager = DBManager.getInstance(getApplication());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        show_analysis.setLayoutManager(linearLayoutManager);



        FoodAttributesBean attrBean01 = new FoodAttributesBean();
        attrBean01.setProgressBar(0);
        attrBean01.setWeightShow("756g");
        attrBean01.setName("重量");
        foodAttributesBeanList.add(attrBean01);

        for (int i = 0; i < 40; i++) {
            FoodAttributesBean attrBean = new FoodAttributesBean();
            attrBean.setProgressBar(i * 40);
            attrBean.setName("不良品");
            foodAttributesBeanList.add(attrBean);
        }


        foodAttriAdapter = new FoodAttriAdapter(ScalesActivity.this, foodAttributesBeanList);
        show_analysis.setAdapter(foodAttriAdapter);

    }
    @OnClick(R.id.admin_food)
    public void adminFood(View view){
        Intent intent =new Intent();
        intent.setClass(ScalesActivity.this,MainActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.init_food)
    public void initFood(View view){

        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.dialog_show_admin, null);
        final EditText editTextName = (EditText) textEntryView.findViewById(R.id.editTextName);
        final EditText editTextNumEditText = (EditText)textEntryView.findViewById(R.id.editTextNum);
        AlertDialog.Builder ad1 = new AlertDialog.Builder(ScalesActivity.this);
        ad1.setTitle("管理员登录:");
        ad1.setView(textEntryView);
        ad1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
                String name =editTextName.getText().toString();
                String pass =editTextNumEditText.getText().toString();
                if(name==null|| TextUtils.isEmpty(name)){
                    ToastUtil.ToastShort(ScalesActivity.this,"请输入名字",2);
                    return;
                }
                if(pass==null||TextUtils.isEmpty(pass)){
                    ToastUtil.ToastShort(ScalesActivity.this,"请输入密码",2);
                    return;
                }

                if(pass.equals(dateFormat.format(new Date()))){
                    ToastUtil.ToastShort(ScalesActivity.this,"登陆成功",1);
                    ToastUtil.createLoadingDialog(ScalesActivity.this,"开始同步数据");
                    initFood();

                }else {
                    ToastUtil.ToastShort(ScalesActivity.this,"密码错误",2);
                }

            }
        });
        ad1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {

            }
        });
        ad1.setCancelable(false);
        ad1.show();// 显示对话框

    }



    private  void initFood(){

        OkGo.<String>get("http://192.168.31.16:8060/att.json")//
                .tag(this)
                //StringCallback只返回成功
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        Response<String> nide = response;
                        FoodInItBean foodInItBean = gson.fromJson(response.body(),
                                new TypeToken<FoodInItBean>() {
                                }.getType());
                        if(foodInItBean.getCode()==0){
                            ToastUtil.closeDialog();
                            List<FoodBean> dataBeanList=foodInItBean.getData();
                            dbManager.clearFoodBean();
                            dbManager.insertUserList(dataBeanList);
                            List<FoodBean> foodBeans = dbManager.queryFoodList();
                            for (FoodBean foodBean:foodBeans){
                                ToastUtil.ToastShort(ScalesActivity.this,foodBean.toString(),2);
                            }

                        }else {
                            ToastUtil.ToastShort(ScalesActivity.this,foodInItBean.getMsg(),2);
                            ToastUtil.closeDialog();
                        }


                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtil.closeDialog();
                        super.onError(response);
                    }
                });



    }
}
